package com.ocn.li.scmdy.modules.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ocn.li.scmdy.common.exception.MyException;
import com.ocn.li.scmdy.common.utils.QueryExt;
import com.ocn.li.scmdy.modules.app.dao.DyUserDao;
import com.ocn.li.scmdy.modules.app.entity.DyUser;
import com.ocn.li.scmdy.modules.app.service.DyUserService;
import com.ocn.li.scmdy.modules.app.utils.ExcelUtils;
import io.swagger.annotations.ApiModelProperty;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.*;

/**
 * @ClassName DyUserServiceImpl
 * @Description TODO
 * @Author Li9527
 * @Date 2020/9/17 13:54
 * @Version 1.0
 */
@Service("dyUserService")
public class DyUserServiceImpl extends ServiceImpl<DyUserDao, DyUser> implements DyUserService {

    @Override
    public Page selectPageUser(QueryExt query) {
        Map map = new HashMap();
        map.putAll(query.getCondition());
        query.setRecords(this.baseMapper.selectPageUser(query, map));
        return query;
    }


    @Override
    public void exportExcel(Map<String, Object> params, HttpServletResponse response) throws IOException {
        //测试使用的数据
        List<DyUser> list = this.selectList(new EntityWrapper<DyUser>().like("name_", "广饶"));
        //传入list集合数组
        List<DyUser> rows = CollUtil.newArrayList(list);
        // 获取当前类字段
        Field[] fields = DyUser.class.getDeclaredFields();
        // 字段名称集合
        List<String> fieldNames = new ArrayList<>();
        // 字段中文名称集合（获取实体中@ApiModelProperty注解value的值）
        List<String> cnNames = new ArrayList<>();
        for (Field field : fields) {
            if (!field.isAccessible()) {
                // 关闭反射访问安全检查，为了提高速度
                field.setAccessible(true);
            }
            String fieldName = field.getName();
            // 排除ID和序号
            if (!"sid".equals(fieldName) && !"serialVersionUID".equals(fieldName) && !"ordernum".equals(fieldName)) {
                fieldNames.add(fieldName);
            }
            // 判断是否有@ApiModelProperty注解
            boolean annotationPresent = field.isAnnotationPresent(ApiModelProperty.class);
            if (annotationPresent && !"sid".equals(fieldName)) {
                ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
                String name = annotation.value();
                cnNames.add(name);
            }
        }
        // 映射
        String[] fs = fieldNames.toArray(new String[0]);
        String[] ns = cnNames.toArray(new String[0]);
//        ExcelWriter writer = ExcelUtil.getWriter();
        ExcelWriter writer = ExcelUtil.getBigWriter("E:/data/excel/LiTest中文.xlsx");
        for(int i = 0; i < ns.length; i++) {
            // 设置表头及字段名
            writer.addHeaderAlias(fs[i], ns[i]);
        }
        writer.setColumnWidth(4, 25);
        writer.write(rows, true);
        writer.close();
//        writer.write(rows, true);
        //自定义标题别名
//        ExcelUtils.write(writer, response, URLUtil.encode(StrUtil.format("ergou{}.xlsx", DateUtil.currentSeconds())));
    }
}